package com.yc.sole.erp.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.yc.sole.common.constant.PubConstant;
import com.yc.sole.common.domain.BaseIndexResp;
import com.yc.sole.erp.dto.SupplierDto;
import com.yc.sole.erp.repository.SupplierRepository;
import com.yc.sole.erp.request.SupplierQueryReq;
import com.yc.sole.erp.request.SupplierReq;
import com.yc.sole.erp.response.SupplierResp;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.service.SequenceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 供应商表 服务接口实现
 *
 * @author yizuomin
 * @date 2023-05-07
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class SupplierBiz {

    private final SupplierRepository supplierRepository;
    private final SequenceService sequenceService;

    /**
     * 新增
     *
     * @param supplierReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveSupplier(SupplierReq supplierReq) {
        SupplierDto dto = new SupplierDto();
        String supplierNo = sequenceService.generateSeqNo(PubConstant.SUPPLIER_SEQ_KEY);
        dto.setSupplierNo(supplierNo);
        dto.setSupplierName(supplierReq.getSupplierName());
        String firstLetter = String.valueOf(PinyinUtil.getFirstLetter(dto.getSupplierName().charAt(0))).toUpperCase(Locale.ROOT);
        dto.setFirstLetter(firstLetter);
        dto.setContacts(supplierReq.getContacts());
        dto.setPhoneNum(supplierReq.getPhoneNum());
        dto.setEmail(supplierReq.getEmail());
        dto.setFax(supplierReq.getFax());
        dto.setAddress(supplierReq.getAddress());
        dto.setTotalNeedPay(null);
        dto.setBeginNeedPay(null);
        dto.setState(supplierReq.getState());
        dto.setRemark(supplierReq.getRemark());
        supplierRepository.saveSupplier(dto);
    }

    /**
     * 修改
     *
     * @param supplierReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateSupplierById(SupplierReq supplierReq) {
        SupplierDto dto = new SupplierDto();
        dto.setId(supplierReq.getId());
        dto.setSupplierName(supplierReq.getSupplierName());
        String firstLetter = String.valueOf(PinyinUtil.getFirstLetter(dto.getSupplierName().charAt(0))).toUpperCase(Locale.ROOT);
        dto.setFirstLetter(firstLetter);
        dto.setContacts(supplierReq.getContacts());
        dto.setPhoneNum(supplierReq.getPhoneNum());
        dto.setEmail(supplierReq.getEmail());
        dto.setFax(supplierReq.getFax());
        dto.setAddress(supplierReq.getAddress());
        dto.setTotalNeedPay(null);
        dto.setBeginNeedPay(null);
        dto.setState(supplierReq.getState());
        dto.setRemark(supplierReq.getRemark());
        BeanUtil.copyProperties(supplierReq, dto);
        supplierRepository.updateSupplierById(dto);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteSupplier(Collection<Long> ids) {
        supplierRepository.deleteSupplierByIds(ids);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param supplierPageReq
     * @return
     */
    public Paging<SupplierResp> selectSupplierPage(Paging<SupplierQueryReq> page, SupplierQueryReq supplierPageReq) {
        Paging<SupplierDto> paging = supplierRepository.selectSupplierPage(page, supplierPageReq);
        return PageUtil.pageCopy(paging, SupplierResp.class);
    }

    /**
     * 根据名称查询所有供应商
     *
     * @param supplierName
     * @return
     */
    public List<SupplierResp> selectSupplierByName(String supplierName) {
        return BeanUtil.copyToList(supplierRepository.selectSupplierByName(supplierName), SupplierResp.class);
    }

    /**
     * 根据id查询所有供应商
     *
     * @param supplierId
     * @return
     */
    public SupplierResp selectSupplierById(Long supplierId) {
        return BeanUtil.copyProperties(supplierRepository.selectSupplierById(supplierId), SupplierResp.class);
    }

    /**
     * 查询带索引的供应商信息
     *
     * @return
     */
    public List<BaseIndexResp<SupplierResp>> selectAllSupplierWithIndex(SupplierQueryReq queryReq) {
        List<SupplierDto> supplierDtoList = supplierRepository.selectAllSupplier(queryReq);
        List<SupplierResp> supplierRespList = BeanUtil.copyToList(supplierDtoList, SupplierResp.class);
        Map<String, List<SupplierResp>> supplierDtoMap = supplierRespList.stream().collect(Collectors.groupingBy(SupplierResp::getFirstLetter));
        return supplierDtoMap.keySet().stream().map(firstLetter -> {
            List<SupplierResp> children = supplierDtoMap.get(firstLetter);
            BaseIndexResp<SupplierResp> indexResp = new BaseIndexResp<>();
            indexResp.setIndex(firstLetter);
            indexResp.setChildren(children);
            return indexResp;
        }).collect(Collectors.toList());
    }
}
